<?include_once('conn.php');?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<? include_once('inc_head.php'); ?>
<?
$act=$_GET['act'];
$tablename=TABLE_C.'admin';
if($act=='editok'){
	$id=$_POST['id'];
	$pass=$_POST['text_pass'];
	if(strlen($_POST['powerItem'])>0){
		$myp='|'.implode("|",$_POST['powerItem']).'|';
	}
	#如果不修改密码
	if(strlen($pass)==0){
		$cmd="update $tablename set power='".$myp."' where id=".$id;
	}
	#要修改密码
	if(strlen($pass)>0){
		if(strlen($pass)<3){#是否小于3位
			gourl("密码不能小于3位数",$cururl);
			exit;
		}else{
			$pass=md5($pass);
			$cmd="update $tablename set pass='".$pass."',power='".$myp."' where id=".$id;
		}
		
	}
	if(mysql_query($cmd) or die(mysql_error())){	
		#如果修改的是当前登录的账号，则马上更新权限
		if($id==$_SESSION['adminId']){
			$_SESSION['adminPower']=$myp;
		}
		gourl("编辑成功",$cururl);
	}
	exit;
}
?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
	.item{color:#666;display:inline-block;width:160px;padding:10px;border:1px solid #f0f0f0; border-radius: 4px;margin:2px;}
	.item:hover{background-color: #f9f9f9;}
	.item label{display: flex!important;align-items: center}
</style>
<script>
	$(function(){
		$("#selectall").on("change",function(){
			var _state = $(this).prop("checked");
			console.log($(".item :checkbox").length);
			$(".item :checkbox").prop("checked",_state)
		});
	});
</script>
<body >
	<?local('用户编辑');?>
<div id="intro">
<?
$cmd="select * from $tablename where id=".intval($_GET['id']);
$data=$mydb->fetchAssoc($cmd);
//得到当前编辑的用户的权限
$mypower=$data['power'];
?> 
<form name="form2" method="post" action="?act=editok">
	<table class="table_form" width="100%" border="0" align="center" cellpadding="0" cellspacing="0" >
		<tr> 
			<td width="100">ID</td>
			<td ><input name="id" type="text" class=" form-control input-md" value="<?echo $data['id']?>" readonly></td>
		</tr>
		<tr> 
			<td >管理名称</td>
			<td > <input name="text_name" type="text"  class=" form-control input-md" value="<?echo $data['name']?>" readonly> </td>
		</tr>
		<tr> 
			<td>密码</td>
			<td > <input name="text_pass" type="text"  value="" class=" form-control input-md"> </td>
		</tr>
		<tr> 
			<td>
				<label class="el-checkbox flex flex-ai-c">
					<input type="checkbox" name="selectall" id="selectall" >
					<span class="el-checkbox-style"></span>
					<span class="pull-right">全选</span>
				</label>
			</td>
			<td >
				<?
				$cmd2 = "select * from ".TABLE_C."admin_power order by id";
				$rspitem  = mysql_query($cmd2);
				while($PO = mysql_fetch_assoc($rspitem)){
					echo "<div class=\"item\">";
					if(strpos($mypower,"|{$PO[id]}|")===false){
						echo "<label class=\"el-checkbox\"><input type=checkbox name='powerItem[]' value=".$PO[id]." ><span class=\"el-checkbox-style\"></span><span>".$PO[name]."</span></label>";
					}else{
						echo "<label class=\"el-checkbox\"><input type=checkbox name='powerItem[]' value=".$PO[id]." checked><span class=\"el-checkbox-style\"></span><span>".$PO[name]."</span></label>";
					}
					echo "</div>";
				}
				mysql_free_result($rspitem);
				?>
			</td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td><button type="submit" class="btn btn-primary">保存</button></td>
		</tr>
	</table>
</form>

</div>
</div>
<?include('inc_foot.php');?>
</body>
</html>